package main

import "fmt"

func main() {
	test_struct()
	fmt.Println("--------------------")
	test_interface()
	fmt.Println("--------------------")
	// 空接口
	m := make(map[string]interface{})
	m["name"] = "Alice"
	m["age"] = 25
	m["gender"] = "female"
	fmt.Println(m["name"])
	fmt.Println(m["age"])
	fmt.Println(m["gender"])
	fmt.Println(m)
	// 类型断言
	if v, ok := m["name"].(string); ok {
		fmt.Println(v)
	}
	// 
}

func test_struct() {
	p := Person{
		Name: "Alice",
		Age:  25,
	}
	p1 := new(Person)
	p1.Name = "Bob"
	p1.Age = 30
	println(p1.SayHello("world"))
	println(p.SayHello("world"))
}

func test_interface() {
	p := Person{
		Name: "Alice",
		Age:  25,
	}
	w := Worker{
		name:   "Bob",
		age:    30,
		gender: "male",
	}
	var pi PersonInterface
	pi = &p
	println("p.getName() ", p.getName())
	println("&p.getName() ", (&p).getName()) // 自动解引用, 等价于 p.getName()
	println("pi.getName() ", pi.getName())
	println("pi.getAge() ", pi.getAge())
	pi = w
	println(pi.getName())
	println(pi.getAge())
	println(pi.getGender())
	w.setName("Tom")
	println(w.getName())
}

type PersonInterface interface {
	getName() string
	getAge() int
	getGender() string
}

type Person struct {
	Name string
	Age  int
}

// 指针接收者
func (p *Person) getName() string {
	return p.Name
}
func (p *Person) getAge() int {
	return p.Age
}
func (p *Person) getGender() string {
	return "default"
}

// 值接收者
func (p Person) SayHello(word string) string {
	return "Hello, " + p.Name + " " + word
}

type Worker struct {
	name   string
	age    int
	gender string
}

func (w Worker) SayHello(word string) string {
	return "Hello, " + w.name + " " + w.gender + " " + word
}
func (w Worker) getGender() string {
	return w.gender
}

func (w Worker) getName() string {
	return w.name
}
func (w *Worker) setName(name string) {
	w.name = name
}
func (w Worker) getAge() int {
	return w.age
}
